<?php
/**
 * 店铺信息模块
 * 
 * @author river_he@ewueang.com
 */
include_once(AMFPHP_BASE . "shared/util/MethodTable.php");
final class ShopService extends Kit_Service{
	/**
	 * 获取商铺信息
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shopId 店id
	 * @return array
	 */
	public function getShop($shoperId, $shopId=0){
		if(1 > $shoperId){
			return $this->pack(101, 'arguments uid error');	
		}
		$sort = '';
		$where = array();
		if($shopId > 0){
			array_push($where, 'id='.$shopId);
		}else{
			$sort = 'id ASC'; //@todo 分店排列先创建的默认显示
		}
		array_push($where, 'nUid='.$shoperId);
		$shop = $this->getShopMod()->getItem($where, $sort);
		if(!$shop){
			return $this->pack(201, 'no shop');
		}
		$shopId = $shop['id'];
		$return = array();
		$return['decos'] = array();
		$return['props'] = array();
		$return['npcs'] = array();
		$shopStat = $this->getShopMod()->getStat('nShopId='.$shopId);
		$shop = array_merge($shop, $shopStat);
		$return['shop'] = $shop;
		$property = $this->getShopMod()->getProps($shopId);
		if($property){
			$return['props'] = $property;
		}
		$decorate = $this->getShopMod()->getDecos($shopId);
		if($decorate){
			$return['decos']= $decorate;
		}
		$npcWhere = array();
		array_push($npcWhere, 'nLevel<='.$shop['nLevel']);
		array_push($npcWhere, 'nOnShelf=1');
		$shopNpc = $this->getNpcMode()->getItems($npcWhere, 'id ASC', 1, 10);
		if($shopNpc){
			$return['npcs'] = $shopNpc['data'];
		}
		return $this->pack(200, 'OK', $return);
	}
	
	/**
	 * 获取储藏室物品列表
	 * 
	 * @param int $shopId 店铺id
	 * @param int $type 物品类别1.升级食材2.道具
	 * @param int $page	页码
	 * @param int $psize 页记录数
	 * @return array
	 */
	public function getStock($shopId, $type, $page, $psize){
		$items = $this->getShopMod()->getStockItems($shopId, $type, $page, $psize);
		$this->pack(200, 'OK', $items);
	}
	
	/**
	 * 获取店铺购买使用的装饰品
	 * 
	 * @param int $shopId 店id
	 * @return array
	 */
	public function getDecos($shopId){
		if(1 > $shopId){
			return $this->pack(101, 'arguments shopId error');	
		}
		$data = $this->getShopMod()->getDecos($shopId);
		return $this->pack(200, 'OK', $data);
	}
	
	/**
	 * 获取店铺使用的道具
	 * 
	 * @param int $shopId 店id
	 * @return array
	 */
	public function getProps($shopId){
		if(1 > $shopId){
			return $this->pack(101, 'arguments shopId error');	
		}
		$data = $this->getShopMod()->getProps($shopId);
		return $this->pack(200, 'OK', $data);
	}
	
	/**
	 * 获取店铺的npc
	 * 
	 * @param int $shoperId
	 * @param int $shopId
	 * @return array
	 */
	public function getShopNpcs($shoperId, $shopId){
		if(1 > $shopId){
			return $this->pack(101, 'arguments shopId error');
		}
		$shop = $this->getShopMod()->getItem('id='.$shopId);
		if($shop){
			if($shop['nUid'] == $shoperId){
				$npcs = array();
				for($i=1; $i<20; $i++){
					array_push($npcs, array(
					'id'=>$i,
					//'nNpcId'=>1,
					'nNpcId'=>1,
					'sFood'=>'1,2,3,4',
					'sMatch'=>'',
					'sName'=>'aa',
					'nShopId'=>$shopId,
					'pace'=>5,
					'wait'=>60,
					'tox'=>0,
					'isBoss'=>floor($i/6),
					'isBuy'=>0
					));
				}
				return $this->pack(200, 'OK', array('data'=>$npcs));
			}
			return $this->pack(201, '该店不属于该店主');
		}
		return $this->pack(202, '该店铺不存在');
	}
	
	/**
	 * 移动饰品
	 * 
	 * @param int $shopDecoId
	 * @param int $px
	 * @param int $py
	 * @return array
	 */
	public function decoMove($shopDecoId, $px, $py){
		if(1 > $shopDecoId){
			return $this->pack(101, 'arguments shopDecoId error');
		}
		$move = $this->getShopMod()->decoMove($shopDecoId, $px, $py);
	}
}
//end